package com.cndw.kungfu.model.goods.app;

import java.util.Map;

import com.cndw.kungfu.config.ConstantsTask;
import com.cndw.kungfu.exception.ErrPopException;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.disciple.PlayerMartial;
import com.cndw.kungfu.model.disciple.domain.Disciple;
import com.cndw.kungfu.model.sys.ChatMain;

/**
 * 打开获得徒弟
 * @author fantadust [9677889@qq.com]
 * @date 2012-3-11 下午4:00:54
 */
public class AppDo109 extends AppBase {

	PlayerMartial playerMartial;

	@Override
	public boolean check() throws Exception {
		playerMartial = Context.getDiscipleServer().getByPlayerId(playerId);
		int canReinNum = player.getDescipleMax() - playerMartial.getDisciples().size();
		if (canReinNum <= 0) {
			throw new ErrPopException(LanguageLib.get("disciple.disciplerein.reachmaxnum"));
		} 
		return true;
	}

	@Override
	public Map<String, Object> run() throws Exception {
		Disciple disciple = Context.getDiscipleServer().createDisciple(playerId, goodsMode.getValueA(),goodsMode.getValueB());
	
		if (disciple == null) {
			throw new ErrPopException(LanguageLib.get("comm.err.sys"));
		}
		
		int id = Context.getDiscipleServer().insertDisciple(disciple);
		if (id > 0) {
			disciple.setId(id);
			playerMartial.addDisciple(disciple);
			
			if (disciple.getQuality() >= 2) {
				if (disciple.getQuality()>=3 ) {
					Context.getTaskServer().notifyToSpark(playerId, ConstantsTask.spark24Rein3, 1);
				}
				
				ChatMain.systemMessage(LanguageLib.get("disciple.reen.broadcast", player.getPlayerName(), disciple.getName(),LangUtil.getColor(disciple.getQuality())));
			}
		}
		msg.put("disciple", disciple.toVO());		
		ticpString = LanguageLib.get("goods.use.disciple",disciple.getName(),LangUtil.getColor(disciple.getQuality()));
		return msg;
	}
	
	
}
